from sklearn import datasets
from sklearn import model_selection
import tensorflow as tf
import numpy as np

# 加载数据
iris = datasets.load_iris()
print(iris.data[:5])

X_train,X_test,y_train,y_test = model_selection.train_test_split(iris.data,iris.target,random_state=1,test_size=0.25)

from sklearn.tree import DecisionTreeClassifier

dc = DecisionTreeClassifier()
dc.fit(X_train,y_train)
print(dc.score(X_train,y_train))
print(dc.score(X_test,y_test))


from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
param_grid = {'n_neighbors':[3,4,5,6]}
grid_knn = GridSearchCV(KNeighborsClassifier(),param_grid,cv=5)
grid_knn.fit(X_train,y_train)
print("最佳参数组合:",grid_knn.best_params_)
print("训练集:",grid_knn.best_score_)
print("测试集",grid_knn.score(X_test,y_test))